﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "NewLevelParameterValidator.h"
#include "DbObjectUtils.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "GbmpDocUtils.h"
#include "GcmpBuiltInParameterDefinitions.h"
#include "IDocument.h"
#include "IElementBasicInformation.h"
#include "IElementParameters.h"
#include "IGenericElement.h"
#include "ILevel.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

DBOBJECT_DATA_DEFINE(NewLevelParameterValidator)
{
}

OwnerPtr<IParameterValidator> NewLevelParameterValidator::Clone() const
{
    return NEW_AS_OWNER_PTR(NewLevelParameterValidator);
}

bool NewLevelParameterValidator::Validate(const IParameter* pNewParameter, const IElement* pElement, std::wstring* pErrorMessage) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pNewParameter && pElement, L"传入参数存在空值",L"GDMPLab",L"2024-03-30");

    const IGenericElement *pGenericElement = quick_cast<const IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");

    const ILevel *pILevel = quick_cast<const ILevel>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pILevel, L"pILevel为空",L"GDMPLab",L"2024-03-30");

    const IElementParameters* pElementParameter = pElement->GetElementParameters();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElementParameter, L"pElementParameters为空",L"GDMPLab",L"2024-03-30");

    if (pNewParameter->GetParameterDefinitionUid() == PARAMETER_UID(ElementNameBuiltInParameter))
    {
        return ValidateName(pElement, pNewParameter->GetValueAsString(), pErrorMessage);
    }

    return true;
}

bool NewLevelParameterValidator::ValidateName(const IElement* pElement, const std::wstring& newName, std::wstring* pErrorMessage) const
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pElement, L"pElement为空",L"GDMPLab",L"2024-03-30");

    IDocument* pDoc = pElement->GetDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    std::vector<IElement*> levelElements = gcmp::GbmpDocUtils::GetElementsByCategory(pDoc, BuiltInCategoryUniIdentities::BICU_NEW_ELEVATION);
    FOR_EACH(pLevelElement, levelElements)
    {
        if (pLevelElement->GetBasicInformation()->GetName() == newName)
        {
            if (pErrorMessage)
            {
                *pErrorMessage = GBMP_TR(L"输入的标高名称已存在，请重新输入。");
            }
            return false;
        }
    }

    return true;
}

//////////////////////////////////////////////////////////////////////////
/// ModelViewParameterValidatorBehavior
DBOBJECT_DATA_DEFINE(NewLevelParameterValidatorOverride)
{
    m_ParameterUid = UniIdentity::InvalidUid;
}

NewLevelParameterValidatorOverride::NewLevelParameterValidatorOverride(UniIdentity parameterUid)
    : m_ParameterUid(parameterUid) 
{

}

UniIdentity NewLevelParameterValidatorOverride::GetUniIdentity() const
{
    return GetParameterUid__();
}

bool NewLevelParameterValidatorOverride::ShouldOverrideIsModifiable() const
{
    return false;
}

bool NewLevelParameterValidatorOverride::IsModifiable() const
{
    return true;
}

bool NewLevelParameterValidatorOverride::ShouldOverrideIsUserVisible() const
{
    return false;
}

bool NewLevelParameterValidatorOverride::IsUserVisible() const
{
    return true;
}

bool NewLevelParameterValidatorOverride::ShouldOverrideGetName() const
{
    return false;
}

std::wstring NewLevelParameterValidatorOverride::GetName() const
{
    return L"";
}

bool NewLevelParameterValidatorOverride::ShouldOverrideGetValidator() const
{
    return true;
}

const IParameterValidator* NewLevelParameterValidatorOverride::GetValidator() const
{
    static NewLevelParameterValidator validator;
    return &validator;
}
